package com.tanhua.mangement.service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.mangement.vo.CommentsVo;
import com.tanhua.mangement.vo.ItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CommentsService {
    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoMapper userInfoMapper;

    public CommentsVo getComments(String pagesize,String page,String sortProp,String sortOrder,String messageID){
        //把分页数据封装对象
        CommentsVo commentsVo=new CommentsVo();
        commentsVo.setPage(Integer.parseInt(page));
        commentsVo.setPagesize(Integer.parseInt(pagesize));

        //查询该动态评论
        List<Comment> commentList = this.quanZiApi.queryCommentsByPublishId(pagesize, page, sortProp, sortOrder, messageID);
        ArrayList<ItemVo> itemVos=new ArrayList<>();
        //查询到评论为空,直接返回
        if (CollectionUtils.isEmpty(commentList)){
            commentsVo.setPages(0);
            commentsVo.setCounts(0);
            return commentsVo;
        }

        //获取评论人的id
        Set<Long> ids = commentList.stream().map(Comment::getUserId).collect(Collectors.toSet());

        //查询评论用户详细信息
        LambdaQueryWrapper<UserInfo> query = new LambdaQueryWrapper<>();
        query.in(UserInfo::getUserId,ids);
        List<UserInfo> userInfos = userInfoMapper.selectList(query);

        //用户信息为空,则直接返回
        if (CollectionUtils.isEmpty(userInfos)){
            commentsVo.setPages(0);
            commentsVo.setCounts(0);
            commentsVo.setItems(itemVos);
            return commentsVo;
        }

        //封装到集合
        for (Comment comment : commentList) {
            ItemVo itemVo=new ItemVo();
            itemVo.setId(comment.getId().toHexString());
            itemVo.setContent(comment.getContent());
//            这里要改下格式
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(comment.getCreated());
            itemVo.setCreateDate(format);

            for (UserInfo userInfo : userInfos) {
                if (comment.getUserId().longValue()==userInfo.getUserId().longValue()){
                    itemVo.setNickname(userInfo.getNickName());
                    itemVo.setUserId(userInfo.getUserId().intValue());
                }
            }
            itemVos.add(itemVo);
        }

        int count = quanZiApi.queryCommentCount(messageID,2 ).intValue();
        commentsVo.setPages(count / Integer.parseInt(pagesize) + 1);
        commentsVo.setCounts(count);
        commentsVo.setItems(itemVos);
        return commentsVo;
    }
}
